package gui.product;

import core.Product;
import core.exception.ModelException;
import gui.common.*;

/**
 * Controller class for the edit product view.
 */
public class EditProductController extends ProductController implements IEditProductController 
{
	/**
	 * Constructor.
	 * 
	 * @param view Reference to the edit product view
	 * @param target Product being edited
	 */
	public EditProductController(IView view, ProductData target) 
	{
		super(view, target);
		
		_pData = target;
		construct();
	}

	/**
	 * Returns a reference to the view for this controller.
	 * 
	 * {@pre None}
	 * 
	 * {@post Returns a reference to the view for this controller.}
	 */
	@Override
	protected IEditProductView getView() 
	{
		return (IEditProductView)super.getView();
	}

	/**
	 * Sets the enable/disable state of all components in the controller's view.
	 * A component should be enabled only if the user is currently
	 * allowed to interact with that component.
	 * 
	 * {@pre None}
	 * 
	 * {@post The enable/disable state of all components in the controller's view
	 * have been set appropriately.}
	 */
	@Override
	protected void enableComponents() 
	{
		try {
			float num = Float.parseFloat(getView().getSizeValue());
			if (!(num > 0F)) {
				getView().enableOK(false);
			}
			else {
				getView().enableOK(true);
			}
		}
		catch (NumberFormatException e) {
			getView().enableOK(false);
		}
		//_pView.enableOK(true);
		_pView.enableBarcode(false);
		_pView.enableDescription(true);
		_pView.enableShelfLife(true);
		if  (_pView.getSizeUnit() == SizeUnits.Count)
		{
			_pView.enableSizeValue(false);
		}
		else
		{
			_pView.enableSizeValue(true);
		}
		_pView.enableSizeUnit(true);
		_pView.enableSupply(true);
	}
	
	@Override
	public void valuesChanged() {
		super.valuesChanged();
		enableComponents();
	}
	
	/**
	 * This method is called when the user clicks the "OK"
	 * button in the edit product view.
	 */
	@Override
	public void editProduct() 
	{
		String barcode = _pData.getBarcode();
		String value = _pData.getSizeValue();
		String unit = _pData.getSizeUnit();
		String desc = _pData.getDescription();
		Product testProduct = null;
		Product productToEdit = _pData.getProduct();
		int shelfLife = 0;
		int supply = 0;
		
		try {
			shelfLife = Integer.parseInt(_pData.getShelfLife());
			supply = Integer.parseInt(_pData.getSupply());
		}
		catch (NumberFormatException e) {
			getView().displayErrorMessage("Non-numeric data");
			return;
		} 
		
		if (Product.canCreate(barcode, desc, value, unit, shelfLife, supply)) {
			testProduct = new Product(barcode, desc, value, unit, shelfLife, supply);
			if(_pFacade.canEditProduct(productToEdit, testProduct)) {
				try {
					_pFacade.editProduct(productToEdit, testProduct);
				}
				catch (ModelException e) {
					getView().displayErrorMessage("Unable to edit product!");
				}
			}
		}
		else {
			getView().displayErrorMessage("Illegal Product data.");
		}

	}

}

